import requests

class RemoteAPI:
    '''
    这部分是用来远程控制车辆的API接口的实现'''

    def __init__(self):
        self.post_back = None
        self.get_back = None
        self.robot_loc_list = {}

    def control(self,payload):
        url = 'http://110.42.175.97:8001/rcs/api/guest/648a1e0c53ececb7e1fc260ba5a47959/robot-control/control'
        

        response = requests.post(url, json=payload)

        if response.status_code == 200:
            data = response.json()
            if data["ok"]:
                print("Operation successful")
                self.post_back = data
            else:
                print("Operation failed. Error message: {}".format(data["msg"]))
        else:
            print("Failed to connect to the server")
        
        return response.json()
        
        



    def status(self):
        url = 'http://110.42.175.97:8001/rcs/api/guest/648a1e0c53ececb7e1fc260ba5a47959/robot-control/status?sceneid=189'
        params = {
            "sceneid": 189
        }

        response = requests.get(url, params=params)

        if response.status_code == 200:
            data = response.json()
            
            if data["ok"]:
                status_list = data["statuslist"]
                self.get_back = status_list
                self.robot_loc_list = self.get_json_parse()
                for status in status_list:
                    print("Robot ID: {}".format(status["robotId"]))
                    print("Position X: {}".format(status["posX"]))
                    print("Position Y: {}".format(status["posY"]))
                    print("Linear Speed: {}".format(status["linearSpeed"]))
                    print("Angular Speed: {}".format(status["angularSpeed"]))
                    print("Chas Orientation: {}".format(status["chasOrientation"]))
                    print("Board Orientation: {}".format(status["boardOrientation"]))
                    print("Status Name: {}".format(status["statusName"]))
                    print("Task ID: {}".format(status["taskid"]))
                    print("\n")
            else:
                print("Operation failed. Error message: {}".format(data["msg"]))
        else:
            print("Failed to connect to the server")

        return response.json()
    
    def get_json_parse(self):
        '''这是从 get_back 中获取 json 数据并解析的函数，主要做的是获取每个机器人的坐标'''

        if self.get_back is None:
            print("Please get the status first")
            return None

        robot_loc_list = {}
        for status in self.get_back:
            robot_id = status["robotId"]
            pos_x = status["posX"]
            pos_y = status["posY"]
            linear_speed = status["linearSpeed"]
            angular_speed = status["angularSpeed"]
            chas_orientation = status["chasOrientation"]
            board_orientation = status["boardOrientation"]
            status_name = status["statusName"]
            task_id = status["taskid"]
            robot_loc_list[robot_id] = (pos_x, pos_y)

        return robot_loc_list

    def post_json_gen(self,robot_id,next):
        '''根据path 为 robot_id 生成 post_back 的 json 数据'''
        payload = {
        "robotid": robot_id,
        "command": "chas-backward {0} {1} 1.0 1".format(next[0],next[1]),
        }
        return payload
    
    

 